<?php
namespace app\backstage\controller;
use think\Controller;
use think\Db;
use think\facade\Request;

class Bank extends Controller
{
    protected $middleware = [
        'VerifyJwt' => ['except' => []],
        'VerifyPower' => ['except' => []],
    ];

    /*列表页面*/
    public function index()
    {
        $ManagerUser = \think\facade\App::model('Account', 'logic');
        $agents = $ManagerUser->getAgents();

        $this->assign('agents',$agents);

        if(request()->_role->alias == 'agent')
        {
            return $this->fetch('index_agent');
        }

        $this->assign('role_name',request()->_role->alias);
        return $this->fetch();
    }

    /*列表数据获取*/
    public function list_data()
    {
        $params = Request::param('', '', 'strip_tags');
        $Bank = \think\facade\App::model('Bank', 'logic');
        $result = $Bank->getList($params);

        if ($result['count'] > 0) {
            return ['code' => 0, 'msg' => '数据获取成功', 'count' => $result['count'], 'data' => $result['list']];
        }

        return ['code' => 1, 'msg' => '暂无数据', 'count' => $result['count'], 'data' => $result['list']];
    }

    /*数据添加*/
    public function add()
    {

        if( Request::isGet() ){

            return $this->fetch();
        }

        $params = Request::post('','','strip_tags');
        $verify_result = $this->validate($params, 'Bank.add');

        if(true !== $verify_result){
            return json(['code'=>101,'msg'=>$verify_result]);
        }

        $Bank = \think\facade\App::model('Bank', 'logic');
        $data = $Bank->dataProcessing($params);
        $result = $Bank->addInfo($data['data']);

        return json($result);
    }

    /*信息预览*/
    public function preview()
    {
        $params = Request::param('', '', 'strip_tags');

        $where[] = ['id','=',$params['id']];

        $Bank = \think\facade\App::model('Bank','logic');
        $info = $Bank->getInfo($where);

        if(!$info){
            $this->error('该信息记录不存在');
        }

        $this->assign('info',$info);
        return $this->fetch();
    }

    /*数据编辑*/
    public function edit()
    {
        $params = Request::post('','','strip_tags,trim');
        $verify_result = $this->validate($params, 'Bank.edit');

        if(true !== $verify_result){
            return json(['code'=>101,'msg'=>$verify_result]);
        }

        $Bank = \think\facade\App::model('Bank','logic');

        $data = $Bank->dataProcessing($params);
        $where[] = ['id','=',$params['id']];
        $where[] = ['status','=',1];
        $result = $Bank->editInfo($where,$data['data']);

        return json($result);
    }

    /*信息预览*/
    public function examine()
    {
        if( Request::isGet() ){
            $params = Request::param('', '', 'strip_tags');

            $where[] = ['id','=',$params['id']];

            $Bank = \think\facade\App::model('Bank','logic');
            $info = $Bank->getInfo($where);

            if(!$info){
                $this->error('该信息记录不存在');
            }

            $this->assign('info',$info);
            return $this->fetch();
        }

        $params = Request::post('','','strip_tags,trim');
        $verify_result = $this->validate($params, 'Bank.info');

        if(true !== $verify_result){
            return json(['code'=>101,'msg'=>$verify_result]);
        }

        $where[] = ['id','=',$params['id']];
        $Bank = \think\facade\App::model('Bank','logic');

        $result = $Bank->examineInfo($where,['status'=>2]);

        return json($result);
    }


}